﻿//-----------------------------------------------------------------------
// <copyright file="SecurityController.cs" company="CronopioDD">
//     Copyright CronopioDD. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace ProyectoGoya.Controllers
{
    using System.Linq;
    using System.Web.Mvc;
    using ProyectoGoya.Models;
    using System.ComponentModel.DataAnnotations;

    /// <summary>
    /// SecurityController class
    /// </summary>
    public class DniValidationAttribute : ValidationAttribute
    {

        #region Public Methods

        /// <summary>
        /// GET: /CheckExistence/123456
        /// </summary>
        /// <param name="value">DNI to be checked</param>
        /// <returns>Whether the DNI exists or not</returns>
        public override bool IsValid(object value)
        {
            using (var db = new ProyectoGOYAContext())
            {
                var dni = value as string;
                Firma firma = db.Firmas.FirstOrDefault(x => x.Dni == dni);
                return firma == null;
            }
        }

        #endregion

    }
}